package com.zto.sch.base.thread;

import org.springframework.util.StopWatch;

import java.util.concurrent.CountDownLatch;

/**
 * @program: sch-diy-parent
 * @description:A,B,C,D.其中D要等到A B C全执行完过后才执行，而且A B C是同步运行的
 * @author: sch
 * @create: 2021-01-09 23:08
 **/
public class ThreadDemo_counntDownLatch {
    public static void main(String[] args) {
        StopWatch stopWatch = new StopWatch("计时器");
        int count = 3;
        CountDownLatch countDownLatch = new CountDownLatch(count);
        Thread threadD = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    countDownLatch.await();
                    System.out.println("All Done,threadD执行了..." + stopWatch.prettyPrint());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        threadD.start();
        stopWatch.start();

        for (char threadName = 'A'; threadName < 'D'; threadName++) {
            final String thN = String.valueOf(threadName);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("thread" + thN + "执行了...");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("thread" + thN + "结束了...");

                    countDownLatch.countDown();
                }
            }).start();
        }
        stopWatch.stop();
    }
}
